package constant.milk.cashbook.house;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.AdapterView.OnItemClickListener;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.AptData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkTextView;

public class HouseNameInsertActivity extends SuperActivity
{
private static final int REQUEST_HOUSE_INSERT = 0;
	
	public static final String HOUSE_NAME = "HOUSE_NAME";
	
	private ListView listView;
	private Button nextButton;
	
	private DBSelectQuery select;
	
	private String items;
	private String id;
	
	private HouseListAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.house_title_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
		nextButton = (Button) findViewById(R.id.nextButton);
	}

	@Override
	protected void setOnClick()
	{
		nextButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, HouseInsertActivity.class);
				intent.putExtra(HouseInsertActivity.HOUSE_ID, id);
				startActivityForResult(intent, REQUEST_HOUSE_INSERT);
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		Intent intent = getIntent();
		String title = intent.getStringExtra(HOUSE_NAME);
		
		setTitle(title);
		
		ArrayList<AptData> list = select.getHouseList(title);
		
		items = list.get(0).getName();
		id = list.get(0).getId();
		
		listView.setOnItemClickListener(onItemClickListener);
		
		adapter = new HouseListAdapter(
				superContext, R.layout.radio_list_item, list);
		listView.setAdapter(adapter);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			AptData data = (AptData) arg0.getItemAtPosition(position);
			
			items = data.getName();
			id = data.getId();
			
			adapter.notifyDataSetChanged();
		}
	};
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_HOUSE_INSERT)
		{
			setResult(resultCode);
			finish();
		}
	}

	/**
	 * 체크카드 리스트 아답터
	 */
	private class HouseListAdapter extends ArrayAdapter<AptData> 
	{
    	
		public HouseListAdapter(Context context, int textViewResourceId, ArrayList<AptData> list) {
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.radio_list_item, null);
			}
			
			AptData data = getItem(position);
			
			MilkTextView nameTextView = (MilkTextView) v.findViewById(R.id.radioItemNameTextView);
			RadioButton radioButton = (RadioButton) v.findViewById(R.id.radioItemRadio);
			
			nameTextView.setTextSize(TEXT_SIZE);
			nameTextView.setText(data.getName());
			
			if (items.equals(data.getName()))
			{
				radioButton.setChecked(true);
				id = data.getId();
			}
			else
			{
				radioButton.setChecked(false);
			}
			
			return v;
		}
	}
	
	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_MY_HOUSE);
		startActivity(intent);
	}
}
